package com.lc.rongzecaifu.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.conn.JsonAccessToken;
import com.lc.rongzecaifu.conn.JsonBanklist;
import com.lc.rongzecaifu.conn.JsonWithPay;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.lc.rongzecaifu.toast.LovelyToast;
import com.lc.rongzecaifu.ui.RegistWebActivity;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2017/3/25.
 */

public class WithPayActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivback;
    private TextView tvtitleright;


    private TextView tv01;
    private TextView tv02;
    private ImageView iv01;
    private ImageView iv02;
    private TextView tv03;
    private EditText et01;
    private EditText et02;
    private LinearLayout ll01;
    private Button bt01;

    private boolean Mode = true;//默认提现模式
    private TextView tvtitle;

    private List<JsonBanklist.Info.ListContent> list = new ArrayList<>();
    private SimpleDraweeView ivlogo;
    private TextView tvname;
    private TextView tvid;
    private ImageView ivsettings01;

    private String bankId, msg;
    private PopupWindow popWindow;
    private WebView wv01;

    private PopupWindow popWindow2;
    public String getRequest;
    private String money = "";

    public String id;
    Double d;

    //false为充值
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withpay);
        initView();
        initMode();
        initListener();
    }

    private void initView() {
        this.wv01 = (WebView) findViewById(R.id.wv_01);
        this.bt01 = (Button) findViewById(R.id.bt_01);
        this.ll01 = (LinearLayout) findViewById(R.id.ll_01);
        this.et02 = (EditText) findViewById(R.id.et_02);
        this.et01 = (EditText) findViewById(R.id.et_01);
        this.tv03 = (TextView) findViewById(R.id.tv_03);
        this.iv02 = (ImageView) findViewById(R.id.iv_02);
        this.iv01 = (ImageView) findViewById(R.id.iv_01);
        this.tv02 = (TextView) findViewById(R.id.tv_02);
        this.tv01 = (TextView) findViewById(R.id.tv_01);


        this.ivsettings01 = (ImageView) findViewById(R.id.iv_settings_01);
        this.tvid = (TextView) findViewById(R.id.tv_id);
        this.tvname = (TextView) findViewById(R.id.tv_name);
        this.ivlogo = (SimpleDraweeView) findViewById(R.id.iv_logo);
        this.tvtitleright = (TextView) findViewById(R.id.tv_title_right);
        this.ivback = (ImageView) findViewById(R.id.iv_back);
        this.tvtitle = (TextView) findViewById(R.id.tv_title);


        wv01.getSettings().setJavaScriptEnabled(true);
        wv01.getSettings().setBuiltInZoomControls(false);
        wv01.getSettings().setUseWideViewPort(true);
        wv01.getSettings().setDisplayZoomControls(false);
        wv01.getSettings().setLoadWithOverviewMode(true);
        wv01.getSettings().setUseWideViewPort(true);
        wv01.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        WebChromeClient chromeClient = new WebChromeClient();
        wv01.setWebChromeClient(chromeClient);
        wv01.setWebViewClient(new WebViewClient());


        tv02.setText(MyApplication.myPreferences.readAccount_money() + "元");

        et01.setSelection(et01.getText().length());


    }

    private void initMode() {

    }

    private void initListener() {


        tvtitleright.setOnClickListener(this);
        bt01.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_bank:

                intent.putExtra("mode", 0);
                intent.setClass(this, ChoseBankActivity.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.ll_add:
                if (MyApplication.myPreferences.readId_status().equals("0")) {
                    //  ToastUtils.show(this, "请先通过实名认证，实名认证通过后方可绑定银行卡。");
                    LovelyToast.makeText(WithPayActivity.this, "请先通过实名认证，实名认证通过后方可绑定银行卡。", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (MyApplication.myPreferences.readId_status().equals("2")) {
                    ToastUtils.show(this, "实名认证审核中，实名认证通过后方可绑定银行卡。");
                } else if (MyApplication.myPreferences.readId_status().equals("1")) {
                    intent.setClass(this, BankcardAddActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tv_title_right:

                intent.putExtra("mode", false);//true为提现，false为充值
                intent.setClass(this, WithdrawOrRechargeRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_01:


            /*     if (MyApplication.myPreferences.readStatus().equals("9")) {
                showPopupWindow2(bt01);
            }else */if (TextUtils.isEmpty(et01.getText().toString().trim())) {
                    LovelyToast.makeText(WithPayActivity.this, "请输入充值金额", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else if (!TextUtils.isEmpty(et01.getText().toString().trim()) && Double.valueOf(et01.getText().toString()).intValue() < 100) {
                    LovelyToast.makeText(WithPayActivity.this, "充值金额最低为100", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);


                } else if (!TextUtils.isEmpty(et01.getText().toString().trim()) && et01.getText().toString().trim().contains(".") && (et01.getText().toString().trim().length() - et01.getText().toString().trim().indexOf(".") - 1) > 2) {
                  /*  String pay_str = et01.getText().toString().trim();
                    int position = pay_str.length() - pay_str.indexOf(".") - 1;
                    if (position > 2) {
                        LovelyToast.makeText(WithPayActivity.this, "您输入的充值数额不对，请重新输入", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    } else {
                        showPopupWindow(bt01);
                    }*/
                    LovelyToast.makeText(WithPayActivity.this, "您输入的充值数额不对，请重新输入", 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);
                } else {
                    showPopupWindow(bt01);
                }





/*
                if () {
                    //  Toast.makeText(context, "请输入充值金额", Toast.LENGTH_SHORT).show();

                } else if (a < 100) {

                } else {


//                    Double d = Double.valueOf(pay_str);
                    if (pay_str.contains(".")) {



                        if (position > 2) {
                            // Toast.makeText(context, "您输入的充值数额不对，请重新输入", Toast.LENGTH_SHORT).show();

                        } else {

                        }
                    } else {
                        showPopupWindow(bt01);
                    }


                }*/


                break;
        }


    }

    private void showPopupWindow(View parent) {
        if (popWindow == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_03, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册

        TextView text_moneycount = (TextView) view.findViewById(R.id.text_moneycount);


        text_moneycount.setText(et01.getText().toString());


        final LinearLayout linear_dialog3 = (LinearLayout) view.findViewById(R.id.linear_dialog3);

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                popWindow.dismiss();
                popWindow = null;
                finish();
            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                money = et01.getText().toString();
                //getRechargeAccessToken(money);
                getRechargeAccessToken(money);

            }
        });
    }

    private void getRechargeAccessToken(final String money) {
        new JsonAccessToken("pay/charge", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {

                        initWithPayvalidate(info.access_token);

                    } else {
                        //  UtilToast.show(WithPayActivity.this, "安全验证失败！");
                        LovelyToast.makeText(WithPayActivity.this, "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                // UtilToast.show(WithPayActivity.this, "安全验证失败！");
                LovelyToast.makeText(WithPayActivity.this, "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(this, true);

    }

    private void initWithPayvalidate(final String access_token) {
        Log.d(MyApplication.myPreferences.readUid(), "-----------------------------------------");

        new JsonWithPay(access_token, MyApplication.myPreferences.readUid(), et01.getText().toString(), "500002", new AsyCallBack<JsonWithPay.Info>() {


            @Override
            public void onSuccess(String toast, int type, final JsonWithPay.Info info) throws Exception {
                if (info != null) {
                    String string = info.url;
                    if (!TextUtils.isEmpty(string)) {
                        Intent intent = new Intent();
                        intent.putExtra("url", string);
                        intent.setClass(WithPayActivity.this, PayWebActivity.class);
                        startActivity(intent);

                    }

                    popWindow.dismiss();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                bt01.setSelected(false);
                msg = JsonWithPay.TOAST;
                if (!TextUtils.isEmpty(msg)) {

                    //   Toast.makeText(WithPayActivity.this, msg, Toast.LENGTH_SHORT).show();
                /*    LovelyToast.makeText(WithPayActivity.this, msg, 0, LovelyToast
                            .SUCCESS, LovelyToast.SCALE);*/
                    popWindow.dismiss();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                popWindow.dismiss();
            }
        }).execute(this, true);


    }


    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
            View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(this)
                    .inflate(R.layout.dialog_kaihu, null)));
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void initPop2(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册


        final LinearLayout linear_dialog3 = (LinearLayout) view.findViewById(R.id.linear_dialog3);

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
finish();
                popWindow2.dismiss();
                popWindow2 = null;

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent();
                intent.putExtra("url", MyApplication.myPreferences.readRegistUrl());
                intent.setClass(WithPayActivity.this, RegistWebActivity.class);
                startActivity(intent);
                popWindow2.dismiss();
            }
        });
    }


}
